package manager
{
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	
	import gameFrame.GameFrame;
	
	import global.event.EventEx;
	

	public class FrameMgr
	{
		private static var instance:FrameMgr;
		
		private var frameList:Object = new Object();  			//模块实例对像列表
		private var mainStage_value:Stage
		private var layerNum:int = 4;
		private var vecLayerList:Vector.<Sprite>;					//模块显示层次
		private var frameClassList:Object = new Object();  	//模块类列表
	
		public function FrameMgr()
		{		
			
			vecLayerList = new Vector.<Sprite>;

			for (var i:int = 0; i < layerNum; i++) 
			{
				var spr:Sprite = new Sprite();
				spr.mouseEnabled = false;
				vecLayerList.push(spr);
			}
			
			EventMgr.inst.addEventListener(EventEx.STAGE_RESIZE, onStageResize);
		}

		public static function get inst():FrameMgr
		{
			return instance ||= new FrameMgr();
		}
		
		
		public function Init(s:Stage, main:Sprite):void
		{
			mainStage = s;

			for (var i:int = 0; i < layerNum; i++) 
			{
				main.addChild(vecLayerList[i]);
			}
			updataLayerSize();
		}
		
		/**
		 * 注册模块到管理器 
		 * @param strName  		模块名称
		 * @param frameClass 	模块类
		 * @param layer	 			模块显示层
		 * @param resList 			模块资源
		 * 
		 */		
		public function registerFrame(strName:String, frameClass:Class, layer:int, resList:Array):void
		{
			var frame:FrameObject = new FrameObject();
			frame.frameName = strName;
			frame.frameClass = frameClass;
			frame.frameLayer = layer;
			frame.frameResList = resList;
			
			frameClassList[strName] = frame;
		}
		
		
		private function onStageResize(event:Event):void
		{
			updataLayerSize();
			updataAlign();
		}

		private function updataLayerSize():void
		{
			for (var i:int = 0; i < layerNum; i++) 
			{
				dreawLayer(vecLayerList[i]);
			}
		}
		
		
		private function updataAlign():void
		{
			for(var item:* in frameList)
			{
				var frame:GameFrame = frameList[item] as GameFrame;
			}
		}		
		
		
		private function dreawLayer(layer:Sprite):void
		{
			layer.graphics.clear();
			layer.graphics.beginFill(0x0,0);
			layer.graphics.drawRect(0, 0, mainStage.stageWidth, mainStage.stageHeight);
			layer.graphics.endFill();
		}

		
		/**
		 *　打开模块 
		 * @param event
		 * 
		 */		
		public function openFrame(strFrameName:String, objData:Object = null):void
		{
			var framename:String = strFrameName;
			
			if(!framename)
				return ;
			
			var frameobj:Object = findFrame(framename);
			
			if(frameobj)
			{
				showFrame(framename, objData);
			}
			else
			{
				var frameData:FrameObject = frameClassList[framename];
				var resList:Array = frameData.frameResList;
				
				//判断有没有资源，有就加载
				if(resList)
				{
					LoadMgr.inst.loaderResource(resList, framename, null, createFrame, null, objData)
				}
				else
				{
					createFrame(framename, objData);
				}
			}
			
		}
		
		/**
		 * 关闭模块 
		 * @param strFrameName
		 * 
		 */		
		public function closeFrame(strFrameName:String):void
		{
			var framename:String = strFrameName;
			
			var frame:Object = findFrame(framename);
			
			if(!frame)
				return;
			
			var frameData:FrameObject = frameClassList[framename];
		
			var layer:int = frameData.frameLayer;
			
			var sprLayer:Sprite = getLayer(layer);
			
			if(sprLayer.contains(frame as Sprite))
			{
				sprLayer.removeChild(frame as Sprite);
			}

			frame.Close();
		}
		
		/**
		 * 创建模块 
		 * @param strName
		 * @param obj
		 * 
		 */		
		private function createFrame(framename:String, obj:Object):void
		{
			var frameobj:GameFrame = findFrame(framename);
			
			if(!frameobj)
			{
				var frameData:FrameObject = frameClassList[framename];
				var cl:Class = frameData.frameClass;
				if(cl)
					frameList[framename] = new cl();
			}
			showFrame(framename, obj);
		}	
		

		/**
		 * 显示模块 
		 * @param strName
		 * @param obj
		 * 
		 */		
		private function showFrame(framename:String, obj:Object):void
		{
			var frameobj:GameFrame = findFrame(framename);
			
			if(frameobj)
			{
				var frameData:FrameObject = frameClassList[framename];
				
				var layer:int = frameData.frameLayer;
				
				var sprLayer:Sprite = getLayer(layer);
				
				sprLayer.addChild(frameobj);
				
				frameobj.show(obj);
			}
		}
		
		/**
		 *　查找模块 
		 * @param str
		 * @return 
		 * 
		 */		
		public function findFrame(str:String):*
		{
			return frameList[str];
		}
		
		
		/**
		 *　模块是否显示在场景中 
		 * @param strName
		 * @return 
		 * 
		 */		
		public function isShow(strName:String):Boolean
		{
			var frameobj:GameFrame = findFrame(strName);
			
			if(frameobj)
			{
				for (var i:int = 0; i < layerNum; i++) 
				{
					var spr:Sprite = vecLayerList[i];
					if(spr.contains(frameobj))
					{
						return true;
					}
				}
			}
			return false;
		}

		/**
		 * 获取显示层 
		 * @param i
		 * @return 
		 * 
		 */		
		public function getLayer(i:int):Sprite
		{
			if(i<0 && i>=layerNum)
				return vecLayerList[layerNum -1];
			
			return vecLayerList[i] as Sprite;
		}

		public function get mainStage():Stage
		{
			return mainStage_value;
		}

		public function set mainStage(value:Stage):void
		{
			mainStage_value = value;
		}

	}
}

class FrameObject
{
	public var frameName:String;
	public var frameClass:Class;
	public var frameLayer:int;
	public var frameResList:Array;
}